package cn.gok.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: 鸭鸭
 * @ClassName:JwtUtil
 * @Package:cn.gok.util
 * @Description:
 * @date: 2021/8/4
 * @time: 20:12
 */
public class JwtUtil {

    public String CreateJwtTest(String username){
        long now = System.currentTimeMillis();//当前时间
        long exp = now + 1000*60*10;//过期时间为1分钟
        JwtBuilder builder = Jwts.builder().setId("1108")
                .setSubject("TangXY")
                .claim("username",username)
                .setIssuedAt(new Date())
                .setExpiration(new Date(exp))
                .signWith(SignatureAlgorithm.HS256,"itcast");
        return builder.compact();
    }
    public String pareJwt(String token){
        Claims claims = Jwts.parser().setSigningKey("itsss").parseClaimsJws(token).getBody();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy‐MM‐dd hh:mm:ss");
        return sdf.format(claims.getExpiration());
    }

//    public static void main(String[] args) {
//
//       System.out.println(new JwtUtil().CreateJwtTest("lisi"));
//    }
}
